package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import db.ConnectionManager;
import forms.HotelForm;

/**
 * 
 * @author Ceren BOZKURT
 * @author Duygu AYAN
 * @author Pelin OZTURK
 */
public class HotelContainer {


	public HotelForm searchHotel(Hotel hotel) {
		HotelForm hotelForm = new HotelForm();
		Vector<Vector<Object>> hotelList = new Vector<Vector<Object>>();
		Vector<String> hotelMetadata = new Vector<String>();
		ConnectionManager mgr = ConnectionManager.getInstance();
		Connection con = mgr.getConnection();

		try {
			Statement st = con.createStatement() ;

			String query;

			switch (hotel.getHotelPrice()) {
			case 0: // 0 means String[0], first element in the list.
				query = "SELECT * FROM HOTEL WHERE LOCATION='" + hotel.getLocation().trim() + "' AND QUALITY='" + hotel.getQuality().trim() + "' AND HOTEL_PRICE<50";	
				break;
			case 1: // 1 means String[1], first element in the list.
				query = "SELECT * FROM HOTEL WHERE LOCATION='" + hotel.getLocation().trim() + "' AND QUALITY='" + hotel.getQuality().trim() + "' AND HOTEL_PRICE BETWEEN 50 AND 100";
				break;
			case 2: // 2 means String[2], first element in the list.
				query = "SELECT * FROM HOTEL WHERE LOCATION='" + hotel.getLocation().trim() + "' AND QUALITY='" + hotel.getQuality().trim() + "' AND HOTEL_PRICE BETWEEN 100 AND 200";
				break;
			case 3:
				query = "SELECT * FROM HOTEL WHERE LOCATION='" + hotel.getLocation().trim() + "' AND QUALITY='" + hotel.getQuality().trim() + "' AND HOTEL_PRICE BETWEEN 200 AND 400";
				break;
			case 4:
				query = "SELECT * FROM HOTEL WHERE LOCATION='" + hotel.getLocation().trim() + "' AND QUALITY='" + hotel.getQuality().trim() + "' AND HOTEL_PRICE>400";
				break;
			default:
				query = "SELECT * FROM HOTEL";
				break;
			}

			System.out.println(query);
			ResultSet rs = st.executeQuery(query);
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			for(int i=1;i<=columnCount;i++)
				hotelMetadata.add(rsmd.getColumnName(i));

			hotelForm.setHotelMetadata(hotelMetadata);

			while (rs.next()) {
				Vector<Object> hVector = new Vector<Object>();
				hVector.add(rs.getString(1));
				hVector.add(rs.getString(2));
				hVector.add(rs.getString(3));
				hVector.add(rs.getInt(4));
				hVector.add(rs.getInt(5));

				System.out.println(hVector.get(0)+" "+ hVector.get(1) +" "+ hVector.get(2) +" "+ hVector.get(3)+" "+ hVector.get(4));

				hotelList.add(hVector);
			}

			hotelForm.setHotelList(hotelList);

			rs.close();
			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			mgr.closeConnection(con);
		}

		// Return object is flightForm which held flightList and resultsetmetadata.
		return hotelForm;
	}
}
